package com.qiwx.pigeon.presenter;

import com.qiwx.pigeon.base.BasePresenter;
import com.qiwx.pigeon.base.IBaseView;
import com.qiwx.pigeon.data.db.CageStatusRecord;
import com.qiwx.pigeon.data.network.ApiManager;
import com.qiwx.pigeon.data.network.BaseListResponse;
import com.qiwx.pigeon.data.network.BaseResponse;
import com.qiwx.pigeon.data.network.ResponseSubscriber;
import com.qiwx.pigeon.data.params.CLZTParam;
import com.qiwx.pigeon.data.params.CageAdjustParam;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.params.CheckEggParam;
import com.qiwx.pigeon.data.params.FertilizeParam;
import com.qiwx.pigeon.data.params.GETCLZTParam;
import com.qiwx.pigeon.data.params.LoseEggParam;
import com.qiwx.pigeon.data.params.PigeonDeathParam;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageRepairBean;
import com.qiwx.pigeon.data.vo.CageStateBean;
import com.qiwx.pigeon.data.vo.CageStatusRecordBean;
import com.qiwx.pigeon.data.vo.FertilizeBean;
import com.qiwx.pigeon.data.vo.SummaryPigeonDeath;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.fragment.produce.DailyOperationFragment;
import com.qiwx.pigeon.viewinterface.ICageView;
import com.qiwx.pigeon.viewinterface.IOperateView;

import java.lang.ref.WeakReference;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.qiwx.pigeon.viewinterface.IOperateView.ADD_TYPE_ADD_PIGEON_DEATH;
import static com.qiwx.pigeon.viewinterface.IOperateView.ADD_TYPE_BFD_BROKEN;
import static com.qiwx.pigeon.viewinterface.IOperateView.ADD_TYPE_CHANGE;
import static com.qiwx.pigeon.viewinterface.IOperateView.ADD_TYPE_CLZT;
import static com.qiwx.pigeon.viewinterface.IOperateView.ADD_TYPE_REPAIR;
import static com.qiwx.pigeon.viewinterface.IOperateView.UPDATE_ADJUST;
import static com.qiwx.pigeon.viewinterface.IOperateView.UPDATE_CHECK_EGG;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-26 09:59
 * 日常操作
 **/
public class OperatePresenter extends BasePresenter<IOperateView> {

    public OperatePresenter(WeakReference<IOperateView> from) {
        super(from);
    }

    public OperatePresenter(IOperateView from) {
        super(from);
    }

    // 查蛋列表
    public void getCheckEggListByPage(CheckEggParam params) {
        ApiManager.getApiServer().getCheckEggListByPage(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse<PageInfo<FertilizeBean>>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse<PageInfo<FertilizeBean>> response) {
                        getView().onCheckEggSuccess(response.data);
                    }

                });
    }

    //获取需要调窝的鸽笼
    public void getCageAdjustListByPage(CheckEggParam params) {
        ApiManager.getApiServer().getCageAdjustListByPage(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse<PageInfo<FertilizeBean>>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse<PageInfo<FertilizeBean>> response) {
                        getView().onCheckEggSuccess(response.data);
                    }

                });
    }

    //下蛋操作
    public void saveLayEgg(FertilizeParam params) {
        ApiManager.getApiServer().saveLayEgg(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse<Integer>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse<Integer> response) {
                        getView().onSaveLayEggSuccess(response.data);
                    }

                });
    }

    //更新查蛋状态
    public void updateCheckEggStatus(FertilizeParam params) {
        ApiManager.getApiServer().updateCheckEggStatus(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse<Integer>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse<Integer> response) {
                        getView().onAddOperate(UPDATE_CHECK_EGG);
                    }

                });
    }

    //添加鸽子死亡
    public void addPigeonDeath(PigeonDeathParam params) {
        ApiManager.getApiServer().addPigeonDeath(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(ADD_TYPE_ADD_PIGEON_DEATH);
                    }

                });
    }

    //存栏状态修改
    public void changeCLZT(CLZTParam params) {
        ApiManager.getApiServer().changeCLZT(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(ADD_TYPE_CLZT);
                    }

                });
    }


    //存栏状态修改
    public void getCLZT(GETCLZTParam params) {
        ApiManager.getApiServer().getCLZT(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse<CageStateBean>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse<CageStateBean> response) {
                        String data = response.data.getAmount();
                    }

                });
    }



    //需调窝
    public void changeCage(CageBeanVo params) {
        ApiManager.getApiServer().changeCage(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(ADD_TYPE_CHANGE);
                    }

                });
    }

    // 蛋损失不孵蛋 踩破蛋 无蛋
    public void addEggLose(LoseEggParam params) {
        ApiManager.getApiServer().addEggLose(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(ADD_TYPE_BFD_BROKEN);
                    }

                });
    }

    // 鸽崽调整
    public void adjustPigeon(CageAdjustParam params) {
        ApiManager.getApiServer().adjustPigeon(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(UPDATE_ADJUST);
                    }

                });
    }

    //添加鸽笼维修
    public void addCageRepair(CageRepairBean params) {
        ApiManager.getApiServer().addCageRepair(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(ADD_TYPE_REPAIR);
                    }

                });
    }

    //删除鸽笼
    public void deleteCageRecord(CageParam params) {
        ApiManager.getApiServer().deleteCageRecord(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse response) {
                        getView().onAddOperate(ADD_TYPE_REPAIR);
                    }

                });
    }

    //更新鸽笼空窝
    public void updateCageKW(CageParam params) {
        ApiManager.getApiServer().updateCageKW(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseResponse<Integer>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseResponse<Integer> response) {
                        getView().onAddOperate(response.data);
                    }

                });
    }

    //获取鸽笼操作记录
    public void getCageStatusRecord(int c_id, String lay_egg_date) {
        FertilizeParam param = new FertilizeParam();
        param.setC_id(c_id);
        param.setLay_egg_date(lay_egg_date);
        ApiManager.getApiServer().getCageStatusRecord(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseSubscriber<BaseListResponse<CageStatusRecordBean>>(true) {
                    @Override
                    public IBaseView getIView() {
                        return getView();
                    }

                    @Override
                    public void onSuccess(BaseListResponse<CageStatusRecordBean> response) {
                        getView().onQueryCateStatusRecordSuccess(response.data);
                    }

                });
    }


}
